
import os,sys
pwd=os.path.dirname(os.path.realpath(__file__))

sys.path.append(pwd+'/../')
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mxshop2.settings")
import django
django.setup()
# django引入完才能引入goods
from goods.models import Goods,GoodsCategory,GoodsImage

from db_tools.data.product_data import row_data
import re
for goods_detail in row_data:
    goods=Goods()
    goods.name=goods_detail["name"]
    goods.market_price=float(re.sub('￥|元','',goods_detail["market_price"]))
    goods.goods_brief=goods_detail['desc'] if goods_detail['desc'] is not None else ""
    goods.shop_price=float(re.sub('￥|元','',goods_detail['sale_price']))
    goods.goods_desc=goods_detail['goods_desc'] if goods_detail['goods_desc'] is not None else ""
    goods.goods_front_image=goods_detail['images'][0] if goods_detail['images'][0] is not None else ""
    category_name=goods_detail['categorys'][-1]
    category=GoodsCategory.objects.filter(name=category_name)
    if category:
        goods.category=category[0]
    goods.save()
    for goods_image in goods_detail['images']:
        goods_image_instance=GoodsImage()
        goods_image_instance.image=goods_image
        goods_image_instance.goods=goods
        goods_image_instance.save()
